package qingxin.servlet.client.feedback;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import qingxin.dao.FeedbackDAO;
import qingxin.factory.DAOFactory;
import qingxin.vo.Feedback;

@WebServlet("/client/feedback/add")
public class AddServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private FeedbackDAO tipDAO;
	
	public AddServlet(){
		super();
		tipDAO = DAOFactory.getFeedbackDAO();
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) 
		    throws ServletException, IOException {
			request.setCharacterEncoding("UTF-8");
	    	response.setContentType("text/html; charset=utf-8");
	    	response.setCharacterEncoding("utf-8");
			PrintWriter writer = response.getWriter();
			
			try{
				String uidS = request.getParameter("uid");			
				String feedback = request.getParameter("feedback");
				Date now = new Date();
				DateFormat df = new SimpleDateFormat( "yyyy-MM-dd hh:mm:ss"); 
				String date = df.format(now);
				
				Feedback tip = new Feedback();
				tip.setContent(feedback);
				tip.setDate(date);
				if(uidS != null){
					tip.setUserID(Integer.parseInt(uidS));
					tipDAO.addWithUID(tip);
				}
				else{
					tipDAO.addWithoutUID(tip);
				}
			
				
				writer.println("{\"msg\":\"ok\"}");
				response.setStatus(HttpServletResponse.SC_OK);
			}
			catch(Exception e){
				response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
				writer.println("{\"msg\":\"wrong\"}");
			}
			finally{
				writer.flush();
				writer.close();
			}
	}
}
